﻿Imports System
Imports System.IO
Imports System.Text

Public Class Main
    Inherits Connect

    Dim FeatureCode As Integer = 0
    Dim FeatureType(100) As Integer
    Dim FeatureNum As Integer
    Dim FeatureChain(100) As Esprit.FeatureChain
    Dim FeaturePtoP(100) As Esprit.FeaturePtop

    Dim Func As Functions = New Functions
    Dim ProcessDataForm As ProcessDataForm = New ProcessDataForm
    Dim MaterialShapeForm As MaterialShapeForm = New MaterialShapeForm
    Dim RoughProcessForm As RoughProcess = New RoughProcess
    Dim SemiFinishingProcessForm As SemiFinishingProcess = New SemiFinishingProcess
    Dim FinishingProcessForm As FinishingProcess = New FinishingProcess
    Dim MaterialSettingForm As MaterialSettingForm = New MaterialSettingForm
    Dim RoughnessSettingForm As RoughnessForm = New RoughnessForm
    Dim NewToolForm As NewToolForm = New NewToolForm

    Dim XMax As Nullable(Of Double) = Nothing
    Dim XMin As Nullable(Of Double) = Nothing
    Dim YMax As Nullable(Of Double) = Nothing
    Dim YMin As Nullable(Of Double) = Nothing
    Dim ZMax As Nullable(Of Double) = Nothing
    Dim ZMin As Nullable(Of Double) = Nothing
    Dim IsDataReusing As Boolean
    Dim IsFeatureCodeSet As Boolean = False
    Dim ProcessOrder As Integer = 0
    Dim IsSemiFinishing As Boolean = False

    Dim ConverterCodeToFCK(99) As Integer
    Dim ConverterFCKToCode(99) As Integer

    Dim ConverterCodeToFPK(99) As Integer
    Dim ConverterFPKToCode(99) As Integer

    Dim Length(99) As Double
    Dim tLength As Double            'フィーチャの長さでの制限をかけるための変数 
    Dim PI As Double = 3.1415926535897949

    '自動で動作
    Public Sub AutoMated()
        ReadShapeData()
        CreateMaterialShape()
        ReadToolInfos()
        Dim lFeatureCode As Integer
        For lFeatureCode = 1 To FeatureNum
            If FeatureType(lFeatureCode) = 0 Then Continue For
            FeatureCode = lFeatureCode
            If FeatureType(lFeatureCode) = 0 Then Exit For
            ProcessOrder = 1
            IsDataReusing = True
            '動画作成用に対象のフィーチャを青にする
            If FeatureType(FeatureCode) = 7 Or FeatureType(FeatureCode) = 8 Or FeatureType(FeatureCode) = 13 Then
                FeaturePtoP(FeatureCode).Color = &HFF0000
            Else
                FeatureChain(FeatureCode).Color = &HFF0000
            End If
            Document.Refresh()                          '動画作成用にドキュメントを更新
            System.Threading.Thread.Sleep(1000)         '動画作成用に一時停止
            With RoughProcessForm
                .Restricting(Length(lFeatureCode))
                .PreLoardForm(lFeatureCode)
                .ShowSimilarShapeCandidate()
                .ReadMachiningData(.gDBModelCode(0), .gDBFeatureCode(0))
                .ReviseMachiningData()
                .AcceptMachiningData()
            End With
            CreateTechnology2()
            Document.Refresh()                          '動画作成用にドキュメントを更新
            System.Threading.Thread.Sleep(1000)         '動画作成用に一時停止
            'フィーチャの色を戻す
            If FeatureType(FeatureCode) = 7 Or FeatureType(FeatureCode) = 8 Or FeatureType(FeatureCode) = 13 Then
                FeaturePtoP(FeatureCode).Color = &HC0C0C0
            Else
                FeatureChain(FeatureCode).Color = &HC0C0C0
            End If
        Next

    End Sub

    'SolidWorksで作成したShapaData.txtの読み込み
    Public Sub ReadShapeData()
        '読み込み用のReader
        Dim path As String = "ShapeData.txt"
        Dim Reader As StreamReader = New StreamReader(path)

        'ShapeDataEsp書き込み用のWriter
        path = "ShapeDataEsp.txt"
        Dim Write As StreamWriter = New StreamWriter(path)

        Dim Coordinate(2) As Double
        Dim StartCoordinate(10, 2) As Double
        Dim EndCoordinate(10, 2) As Double
        Dim retval(10) As String
        Dim Read As String
        Dim P(9) As Esprit.Point        'この段階では，頂点を10以上持つ面は想定しない
        Dim FeatureCounter As Integer
        Dim EdgeNum, EdgeCounter As Integer
        Dim StartingPoint, EndPoint As Esprit.Point
        Dim CurveType(10) As Integer
        Dim EdgeType(10) As Integer
        Dim Name As String
        Dim FaceNum As Integer
        Dim FeatureTypeName As String
        Dim Key As String = ""
        Dim Segments(10) As Esprit.Segment                      'MEMO 130713 現段階では，10以上のSegmentの数が１０以上の場合を想定していない．
        Dim Arcs(10) As Esprit.Arc                              'MEMO 130713 現段階では，10以上のArcの数が１０以上の場合を想定していない．
        Dim CenterPoint As Esprit.Point
        Dim Radious, StartAngle, EndAngle As Double
        Dim FeatureChainStartingPoint As Esprit.Point
        Dim flag As Boolean = True
        Dim ArcInfo(10, 6) As Double
        Dim ArcCounter As Integer
        Dim ChainStart(10, 2), ChainEnd(10, 2) As Double
        Dim Depth As Double
        Dim IsInside As Boolean
        Dim IsUpside As Boolean
        Dim CurveTypeLoop(100) As Integer
        Dim tZMax As Double
        Dim tZMin As Double
        Dim FeatureCode As Integer
        'Dim FeatureChain As Esprit.FeatureChain
        Dim OpenEdgeItemNum(3) As Integer



        Read = Reader.ReadLine
        retval = Read.Split(CChar(" "))
        FeatureNum = CInt(retval(0))
        Write.Write(CStr(FeatureNum) + " " + vbCrLf)

        For FeatureCounter = 0 To FeatureNum - 1
            Read = Reader.ReadLine
            retval = Read.Split(CChar(" "))

            FeatureCode = CInt(retval(0))
            FeatureType(FeatureCode) = CInt(retval(1))

            FeatureTypeName = Func.NameOfFeature(FeatureType(FeatureCode))
            FaceNum = Func.NumberOfFacesInFeature(FeatureType(FeatureCode))
            IsInside = Func.IsInSideOrNot(FeatureType(FeatureCode))
            IsUpside = Func.IsUpSideOrNot(FeatureType(FeatureCode))

            Write.Write(CStr(FeatureCode) + " " + CStr(FeatureType(FeatureCode)) + vbCrLf)

            ArcCounter = 0
            Read = Reader.ReadLine
            retval = Read.Split(CChar(" "))

            EdgeNum = CInt(retval(0))

            For EdgeCounter = 0 To EdgeNum - 1
                Read = Reader.ReadLine
                retval = Read.Split(CChar(" "))

                CurveType(EdgeCounter) = CInt(retval(1))
                If EdgeCounter = 0 Then CurveTypeLoop(FeatureCode) = CurveType(EdgeCounter)

                Select Case CurveType(EdgeCounter)
                    Case 1, 2
                        'Segmentの場合(Convex/ Concave Edge)
                        'Segmentの情報を取得
                        Read = Reader.ReadLine
                        retval = Read.Split(CChar(" "))

                        StartCoordinate(EdgeCounter, 0) = CDbl(retval(0)) * 1000
                        StartCoordinate(EdgeCounter, 1) = CDbl(retval(1)) * 1000
                        StartCoordinate(EdgeCounter, 2) = CDbl(retval(2)) * 1000

                        EndCoordinate(EdgeCounter, 0) = CDbl(retval(3)) * 1000
                        EndCoordinate(EdgeCounter, 1) = CDbl(retval(4)) * 1000
                        EndCoordinate(EdgeCounter, 2) = CDbl(retval(5)) * 1000

                        '製品形状を覆う形状を作る（X,Y,ZのMaxとMinを探す）
                        If XMax Is Nothing Then
                            If StartCoordinate(EdgeCounter, 0) > EndCoordinate(EdgeCounter, 0) Then
                                XMax = StartCoordinate(EdgeCounter, 0)
                                XMin = EndCoordinate(EdgeCounter, 0)
                            Else
                                XMax = EndCoordinate(EdgeCounter, 0)
                                XMin = StartCoordinate(EdgeCounter, 0)
                            End If
                            If StartCoordinate(EdgeCounter, 1) > EndCoordinate(EdgeCounter, 1) Then
                                YMax = StartCoordinate(EdgeCounter, 1)
                                YMin = EndCoordinate(EdgeCounter, 1)
                            Else
                                YMax = EndCoordinate(EdgeCounter, 1)
                                YMin = StartCoordinate(EdgeCounter, 1)
                            End If
                            If StartCoordinate(EdgeCounter, 2) > EndCoordinate(EdgeCounter, 2) Then
                                ZMax = StartCoordinate(EdgeCounter, 2)
                                ZMin = EndCoordinate(EdgeCounter, 2)
                            Else
                                ZMax = EndCoordinate(EdgeCounter, 2)
                                ZMin = StartCoordinate(EdgeCounter, 2)
                            End If
                        End If

                        If EdgeCounter = 0 Then
                            If StartCoordinate(EdgeCounter, 2) > EndCoordinate(EdgeCounter, 2) Then
                                tZMax = StartCoordinate(EdgeCounter, 2)
                                tZMin = EndCoordinate(EdgeCounter, 2)
                            Else
                                tZMax = EndCoordinate(EdgeCounter, 2)
                                tZMin = StartCoordinate(EdgeCounter, 2)
                            End If
                        End If

                        If StartCoordinate(EdgeCounter, 0) < EndCoordinate(EdgeCounter, 0) Then
                            If XMax < EndCoordinate(EdgeCounter, 0) Then
                                XMax = EndCoordinate(EdgeCounter, 0)
                            End If
                            If XMin > StartCoordinate(EdgeCounter, 0) Then
                                XMin = StartCoordinate(EdgeCounter, 0)
                            End If
                        Else
                            If XMax < StartCoordinate(EdgeCounter, 0) Then
                                XMax = StartCoordinate(EdgeCounter, 0)
                            End If
                            If XMin > EndCoordinate(EdgeCounter, 0) Then
                                XMin = EndCoordinate(EdgeCounter, 0)
                            End If
                        End If

                        If StartCoordinate(EdgeCounter, 1) < EndCoordinate(EdgeCounter, 1) Then
                            If YMax < EndCoordinate(EdgeCounter, 1) Then
                                YMax = EndCoordinate(EdgeCounter, 1)
                            End If
                            If YMin > StartCoordinate(EdgeCounter, 1) Then
                                YMin = StartCoordinate(EdgeCounter, 1)
                            End If
                        Else
                            If YMax < StartCoordinate(EdgeCounter, 1) Then
                                YMax = StartCoordinate(EdgeCounter, 1)
                            End If
                            If YMin > EndCoordinate(EdgeCounter, 1) Then
                                YMin = EndCoordinate(EdgeCounter, 1)
                            End If
                        End If

                        If StartCoordinate(EdgeCounter, 2) < EndCoordinate(EdgeCounter, 2) Then
                            If tZMax < EndCoordinate(EdgeCounter, 2) Then
                                tZMax = EndCoordinate(EdgeCounter, 2)
                            End If
                            If tZMin > StartCoordinate(EdgeCounter, 2) Then
                                tZMin = StartCoordinate(EdgeCounter, 2)
                            End If
                        Else
                            If tZMax < StartCoordinate(EdgeCounter, 2) Then
                                tZMax = StartCoordinate(EdgeCounter, 2)
                            End If
                            If tZMin > EndCoordinate(EdgeCounter, 2) Then
                                tZMin = EndCoordinate(EdgeCounter, 2)
                            End If
                        End If

                    Case 3
                        'Arcの場合
                        'Arcの情報を取得
                        ArcCounter += 1
                        Read = Reader.ReadLine
                        retval = Read.Split(CChar(" "))
                        StartCoordinate(EdgeCounter, 0) = CDbl(retval(0)) * 1000
                        StartCoordinate(EdgeCounter, 1) = CDbl(retval(1)) * 1000
                        StartCoordinate(EdgeCounter, 2) = CDbl(retval(2)) * 1000

                        EndCoordinate(EdgeCounter, 0) = CDbl(retval(3)) * 1000
                        EndCoordinate(EdgeCounter, 1) = CDbl(retval(4)) * 1000
                        EndCoordinate(EdgeCounter, 2) = CDbl(retval(5)) * 1000

                        ArcInfo(EdgeCounter, 0) = CDbl(retval(6)) * 1000    '中心x    
                        ArcInfo(EdgeCounter, 1) = CDbl(retval(7)) * 1000    '中心ｙ    
                        ArcInfo(EdgeCounter, 2) = CDbl(retval(8)) * 1000    '中心ｚ
                        ArcInfo(EdgeCounter, 3) = CDbl(retval(9)) * 1000    '半径
                        ArcInfo(EdgeCounter, 4) = CDbl(retval(10))          '開始角度
                        ArcInfo(EdgeCounter, 5) = CDbl(retval(11))          '終了角度
                        'SWとEspritのπの桁数の調整（２πのときのみ）
                        'TODO 根本的なπの問題の解決
                        If ArcInfo(EdgeCounter, 4) = 6.28319 Then ArcInfo(EdgeCounter, 4) = 2 * PI
                        If ArcInfo(EdgeCounter, 5) = 6.28319 Then ArcInfo(EdgeCounter, 5) = 2 * PI


                        Dim tXMax, tXMin, tYMax, tYMin As Double
                        Dim Center(1) As Double
                        Center(0) = ArcInfo(EdgeCounter, 0)
                        Center(1) = ArcInfo(EdgeCounter, 1)
                        tZMax = ArcInfo(EdgeCounter, 2)
                        tZMin = ArcInfo(EdgeCounter, 2)
                        StartAngle = ArcInfo(EdgeCounter, 4)
                        EndAngle = ArcInfo(EdgeCounter, 5)

                        '製品形状を覆う形状を作成(x,y,zの最大，最小を探す)
                        If Math.Cos(StartAngle) > Math.Cos(EndAngle) Then : tXMax = Center(0) + Radious * Math.Cos(StartAngle)
                        Else : tXMax = Center(0) + Radious * Math.Cos(EndAngle)
                        End If

                        If StartAngle < PI And EndAngle > PI Then
                            tXMin = Center(0) + Radious * Math.Cos(PI)
                        Else
                            If Math.Cos(StartAngle) < Math.Cos(EndAngle) Then : tXMin = Center(0) + Radious * Math.Cos(StartAngle)
                            Else : tXMin = Center(0) + Radious * Math.Cos(EndAngle)
                            End If
                        End If

                        If StartAngle < PI / 2 And EndAngle > PI / 2 Then
                            tYMax = Center(1) + Radious * Math.Sin(PI / 2)
                        Else
                            If Math.Sin(StartAngle) > Math.Sin(EndAngle) Then : tYMax = Center(1) + Radious * Math.Sin(StartAngle)
                            Else : tYMax = Center(1) + Radious * Math.Sin(EndAngle)
                            End If
                        End If

                        If StartAngle < PI * 3 / 2 And EndAngle > PI * 3 / 2 Then
                            tYMin = Center(1) + Radious * Math.Sin(PI * 3 / 2)
                        Else
                            If Math.Sin(StartAngle) < Math.Sin(EndAngle) Then : tYMin = Center(1) + Radious * Math.Sin(StartAngle)
                            Else : tYMin = Center(1) + Radious * Math.Sin(EndAngle)
                            End If
                        End If

                        '製品形状を覆う形状を作る（X,Y,ZのMaxとMinを探す）
                        If XMax Is Nothing Then
                            If StartCoordinate(EdgeCounter, 0) > EndCoordinate(EdgeCounter, 0) Then
                                XMax = StartCoordinate(EdgeCounter, 0)
                                XMin = EndCoordinate(EdgeCounter, 0)
                            Else
                                XMax = EndCoordinate(EdgeCounter, 0)
                                XMin = StartCoordinate(EdgeCounter, 0)
                            End If
                            If StartCoordinate(EdgeCounter, 1) > EndCoordinate(EdgeCounter, 1) Then
                                YMax = StartCoordinate(EdgeCounter, 1)
                                YMin = EndCoordinate(EdgeCounter, 1)
                            Else
                                YMax = EndCoordinate(EdgeCounter, 1)
                                YMin = StartCoordinate(EdgeCounter, 1)
                            End If
                            If StartCoordinate(EdgeCounter, 2) > EndCoordinate(EdgeCounter, 2) Then
                                ZMax = StartCoordinate(EdgeCounter, 2)
                                ZMin = EndCoordinate(EdgeCounter, 2)
                            Else
                                ZMax = EndCoordinate(EdgeCounter, 2)
                                ZMin = StartCoordinate(EdgeCounter, 2)
                            End If
                        End If

                        If tXMax > XMax Then XMax = tXMax
                        If tYMax > YMax Then YMax = tYMax
                        If tXMin < XMin Then XMin = tXMin
                        If tYMin < YMin Then YMin = tYMin

                End Select
            Next

            'ClosedSlotとSquareHoleのRestrictLengthを取得（全Edgeの中で一番短いEdgeを取得）
            If FeatureType(FeatureCode) = 1 Or FeatureType(FeatureCode) = 3 Then
                For i As Integer = 0 To 3
                    tLength = Math.Sqrt(Math.Pow(StartCoordinate(i, 0) - EndCoordinate(i, 0), 2) + _
                                        Math.Pow(StartCoordinate(i, 1) - EndCoordinate(i, 1), 2) + _
                                        Math.Pow(StartCoordinate(i, 2) - EndCoordinate(i, 2), 2))
                    If i = 0 Then
                        Length(FeatureCode) = tLength
                    Else
                        If tLength < Length(FeatureCode) Then
                            Length(FeatureCode) = tLength
                        End If
                    End If
                Next
            End If

            'BlindHoleとThroughHoleのRestrictLengthを取得（円の直径）
            If FeatureType(FeatureCode) = 8 Or FeatureType(FeatureCode) = 7 Then
                Length(FeatureCode) = ArcInfo(0, 3) * 2
            End If

            'OpenSlotの場合のRestictLengthを取得（3番目と4番目のEdgeの短い方を取得）
            If FeatureType(FeatureCode) = 5 Then
                Length(FeatureCode) = Math.Sqrt(Math.Pow(StartCoordinate(2, 0) - EndCoordinate(2, 0), 2) + _
                                   Math.Pow(StartCoordinate(2, 1) - EndCoordinate(2, 1), 2) + _
                                   Math.Pow(StartCoordinate(2, 2) - EndCoordinate(2, 2), 2))

                tLength = Math.Sqrt(Math.Pow(StartCoordinate(3, 0) - EndCoordinate(3, 0), 2) + _
                                    Math.Pow(StartCoordinate(3, 1) - EndCoordinate(3, 1), 2) + _
                                    Math.Pow(StartCoordinate(3, 2) - EndCoordinate(3, 2), 2))

                If tLength < Length(FeatureCode) Then Length(FeatureCode) = tLength
            End If

            'Closed SlotのRestrictingLengthを取得（4番目のEdgeを取得）
            If FeatureType(FeatureCode) = 4 Then
                Length(FeatureCode) = Math.Sqrt(Math.Pow(StartCoordinate(3, 0) - EndCoordinate(3, 0), 2) + _
                                   Math.Pow(StartCoordinate(3, 1) - EndCoordinate(3, 1), 2) + _
                                   Math.Pow(StartCoordinate(3, 2) - EndCoordinate(3, 2), 2))
            End If

            'フィーチャの名前を取得
            Name = CStr(FeatureCode) + " " + FeatureTypeName

            'FeatureChainのStartingPointを設定
            Select Case CurveType(0)
                Case 1, 2
                    'Segmentの場合
                    StartingPoint = Document.Points.Add(StartCoordinate(0, 0), StartCoordinate(0, 1), StartCoordinate(0, 2))
                    EndPoint = Document.Points.Add(EndCoordinate(0, 0), EndCoordinate(0, 1), EndCoordinate(0, 2))
                    EdgeType(0) = CurveType(0)

                    ChainStart(0, 0) = StartCoordinate(0, 0)
                    ChainStart(0, 1) = StartCoordinate(0, 1)
                    ChainStart(0, 2) = StartCoordinate(0, 2)

                    ChainEnd(0, 0) = EndCoordinate(0, 0)
                    ChainEnd(0, 1) = EndCoordinate(0, 1)
                    ChainEnd(0, 2) = EndCoordinate(0, 2)

                    FeatureChainStartingPoint = StartingPoint
                    Segments(0) = Func.CreateSegment(StartingPoint, EndPoint)

                Case 3
                    'Arcの場合
                    CenterPoint = Document.Points.Add(ArcInfo(0, 0), ArcInfo(0, 1), ArcInfo(0, 2))
                    Radious = ArcInfo(0, 3)
                    StartAngle = ArcInfo(0, 4)
                    EndAngle = ArcInfo(0, 5)
                    EdgeType(0) = CurveType(0)

                    ChainStart(0, 0) = StartCoordinate(0, 0)
                    ChainStart(0, 1) = StartCoordinate(0, 1)
                    ChainStart(0, 2) = StartCoordinate(0, 2)

                    ChainEnd(0, 0) = EndCoordinate(0, 0)
                    ChainEnd(0, 1) = EndCoordinate(0, 1)
                    ChainEnd(0, 2) = EndCoordinate(0, 2)

                    StartingPoint = Document.Points.Add(StartCoordinate(0, 0), StartCoordinate(0, 1), StartCoordinate(0, 2))
                    FeatureChainStartingPoint = StartingPoint
                    Arcs(0) = Func.CreateArc(CenterPoint, Radious, StartAngle, EndAngle)
            End Select

            'FeatureChainがStartとEndでつながるように並び替え
            For i = 0 To EdgeNum - 1
                flag = True
                For j = 0 To EdgeNum - 1
                    If ChainEnd(i, 0) = StartCoordinate(j, 0) And _
                        ChainEnd(i, 1) = StartCoordinate(j, 1) And _
                        ChainEnd(i, 2) = StartCoordinate(j, 2) Then
                        'i番目のStartとj番目のEndがつながる場合

                        If ChainStart(i, 0) = EndCoordinate(j, 0) And _
                        ChainStart(i, 1) = EndCoordinate(j, 1) And _
                        ChainStart(i, 2) = EndCoordinate(j, 2) Then Continue For

                        flag = False        'EndとEndがつながる可能性のflagをおる
                        Select Case CurveType(j)
                            Case 1, 2
                                'Segmentの場合
                                StartingPoint = Document.Points.Add(StartCoordinate(j, 0), StartCoordinate(j, 1), StartCoordinate(j, 2))
                                EndPoint = Document.Points.Add(EndCoordinate(j, 0), EndCoordinate(j, 1), EndCoordinate(j, 2))

                                Segments(i + 1) = Func.CreateSegment(StartingPoint, EndPoint)
                                EdgeType(i + 1) = CurveType(j)

                                ChainStart(i + 1, 0) = StartCoordinate(j, 0)
                                ChainStart(i + 1, 1) = StartCoordinate(j, 1)
                                ChainStart(i + 1, 2) = StartCoordinate(j, 2)

                                ChainEnd(i + 1, 0) = EndCoordinate(j, 0)
                                ChainEnd(i + 1, 1) = EndCoordinate(j, 1)
                                ChainEnd(i + 1, 2) = EndCoordinate(j, 2)

                            Case 3
                                'Arcの場合
                                CenterPoint = Document.Points.Add(ArcInfo(j, 0), ArcInfo(j, 1), ArcInfo(j, 2))
                                Radious = ArcInfo(j, 3)
                                StartAngle = ArcInfo(j, 4)
                                EndAngle = ArcInfo(j, 5)

                                Arcs(i + 1) = Func.CreateArc(CenterPoint, Radious, StartAngle, EndAngle)
                                EdgeType(i + 1) = CurveType(j)

                                ChainStart(i + 1, 0) = StartCoordinate(j, 0)
                                ChainStart(i + 1, 1) = StartCoordinate(j, 1)
                                ChainStart(i + 1, 2) = StartCoordinate(j, 2)

                                ChainEnd(i + 1, 0) = EndCoordinate(j, 0)
                                ChainEnd(i + 1, 1) = EndCoordinate(j, 1)
                                ChainEnd(i + 1, 2) = EndCoordinate(j, 2)

                        End Select
                    End If
                Next
                'StartとEndで繋げない場合（EndとEndでつながる場合）
                If flag Then
                    For j = 0 To EdgeNum - 1
                        If ChainEnd(i, 0) = EndCoordinate(j, 0) And _
                            ChainEnd(i, 1) = EndCoordinate(j, 1) And _
                            ChainEnd(i, 2) = EndCoordinate(j, 2) Then
                            'i番目のEndとj番目のEndがつながる場合

                            If ChainStart(i, 0) = StartCoordinate(j, 0) And _
                            ChainStart(i, 1) = EndCoordinate(j, 1) And _
                            ChainStart(i, 2) = EndCoordinate(j, 2) Then Continue For

                            Select Case CurveType(j)
                                Case 1, 2
                                    'Segmentの場合
                                    EndPoint = Document.Points.Add(StartCoordinate(j, 0), StartCoordinate(j, 1), StartCoordinate(j, 2))
                                    StartingPoint = Document.Points.Add(EndCoordinate(j, 0), EndCoordinate(j, 1), EndCoordinate(j, 2))

                                    Segments(i + 1) = Func.CreateSegment(StartingPoint, EndPoint)
                                    EdgeType(i + 1) = CurveType(j)

                                    '***StartとEndを逆に入力***
                                    ChainStart(i + 1, 0) = EndCoordinate(j, 0)
                                    ChainStart(i + 1, 1) = EndCoordinate(j, 1)
                                    ChainStart(i + 1, 2) = EndCoordinate(j, 2)

                                    ChainEnd(i + 1, 0) = StartCoordinate(j, 0)
                                    ChainEnd(i + 1, 1) = StartCoordinate(j, 1)
                                    ChainEnd(i + 1, 2) = StartCoordinate(j, 2)

                                Case 3
                                    'Arcの場合
                                    CenterPoint = Document.Points.Add(ArcInfo(j, 0), ArcInfo(j, 1), ArcInfo(j, 2))
                                    Radious = ArcInfo(j, 3)
                                    StartAngle = ArcInfo(j, 4)
                                    EndAngle = ArcInfo(j, 5)

                                    Arcs(i + 1) = Func.CreateArc(CenterPoint, Radious, StartAngle, EndAngle)
                                    EdgeType(i + 1) = CurveType(j)

                                    '***StartとEndを逆に入力***
                                    ChainStart(i + 1, 0) = EndCoordinate(i + 1, 0)
                                    ChainStart(i + 1, 1) = EndCoordinate(i + 1, 1)
                                    ChainStart(i + 1, 2) = EndCoordinate(i + 1, 2)

                                    ChainEnd(i + 1, 0) = StartCoordinate(i + 1, 0)
                                    ChainEnd(i + 1, 1) = StartCoordinate(i + 1, 1)
                                    ChainEnd(i + 1, 2) = StartCoordinate(i + 1, 2)

                            End Select
                        End If
                    Next
                End If
            Next
            Read = Reader.ReadLine
            retval = Read.Split(CChar(" "))

            Depth = CDbl(retval(0)) * 1000

            If IsUpside Then
                tZMax += Depth
            Else
                tZMin -= Depth
            End If

            If ZMax Is Nothing Then
                ZMax = tZMax
                ZMin = tZMin
            Else
                If ZMax < tZMax Then
                    ZMax = tZMax
                End If
                If ZMin > tZMin Then
                    ZMin = tZMin
                End If
            End If

            Dim IsThrough As Boolean
            Select Case FeatureType(FeatureCode)
                Case 7  'Through Hole
                    IsThrough = True
                Case 8  'Blind Hole
                    IsThrough = False
            End Select


            'FeaturePtoPを作成する
            If FeatureType(FeatureCode) = 7 Or FeatureType(FeatureCode) = 8 Then
                FeaturePtoP(FeatureCode) = Func.CreateFeaturePtoP(CenterPoint, Radious, Depth, Name, True, Key)
                Dim XMax, XMin, YMax, YMin As Double
                Dim ZValue As Double
                Dim Center(1) As Double
                Center(0) = ArcInfo(EdgeCounter - 1, 0)
                Center(1) = ArcInfo(EdgeCounter - 1, 1)
                ZValue = ArcInfo(EdgeCounter - 1, 2)

                If Math.Cos(StartAngle) > Math.Cos(EndAngle) Then : XMax = Center(0) + Radious * Math.Cos(StartAngle)
                Else : XMax = Center(0) + Radious * Math.Cos(EndAngle)
                End If

                If StartAngle < PI And EndAngle > PI Then
                    XMin = Center(0) + Radious * Math.Cos(PI)
                Else
                    If Math.Cos(StartAngle) < Math.Cos(EndAngle) Then : XMin = Center(0) + Radious * Math.Cos(StartAngle)
                    Else : XMin = Center(0) + Radious * Math.Cos(EndAngle)
                    End If
                End If

                If StartAngle < PI / 2 And EndAngle > PI / 2 Then
                    YMax = Center(1) + Radious * Math.Sin(PI / 2)
                Else
                    If Math.Sin(StartAngle) > Math.Sin(EndAngle) Then : YMax = Center(1) + Radious * Math.Sin(StartAngle)
                    Else : YMax = Center(1) + Radious * Math.Sin(EndAngle)
                    End If
                End If

                If StartAngle < PI * 3 / 2 And EndAngle > PI * 3 / 2 Then
                    YMin = Center(1) + Radious * Math.Sin(PI * 3 / 2)
                Else
                    If Math.Sin(StartAngle) < Math.Sin(EndAngle) Then : YMin = Center(1) + Radious * Math.Sin(StartAngle)
                    Else : YMin = Center(1) + Radious * Math.Sin(EndAngle)
                    End If
                End If
                'FeaturePtoPではこれは記述しない．
                'Write.Write(Key + " " + CStr(CurveType(0)) + " 2" + vbCrLf)
                'Write.Write(CStr(XMax) + " " + CStr(YMax) + " " + CStr(ZValue) + vbCrLf)
                'Write.Write(CStr(XMin) + " " + CStr(YMin) + " " + CStr(ZValue) + vbCrLf)
                ConverterCodeToFPK(FeatureCode) = Integer.Parse(Key)
                ConverterFPKToCode(Integer.Parse(Key)) = FeatureCode
                'Write.Write(CStr(Depth) + vbCrLf)
                'WriterC2K.Write(CStr(FeatureCode) + " " + CStr(Key) + vbCrLf)
                'WriterK2T.Write(CStr(Key) + " " + CStr(FeatureType) + vbCrLf)
            End If

            Select Case CurveTypeLoop(FeatureCode)
                'CurveTypeに応じてFeatureChain を作成
                'MEMO 130717 現時点では，SegmentとArcの複合FeatureChainは作成出来ない（この部分のSelectも最後のEdgeのCurveTypeで判断）
                Case 1, 2
                    FeatureChain(FeatureCode) = Func.CreateFeatureChainSegment(EdgeNum, FeatureChainStartingPoint, Segments, Name, Key, Depth, IsInside, IsUpside)
                    Write.Write(CStr(FeatureCode) + " " + CStr(CurveType(0)) + " " + CStr(EdgeNum) + vbCrLf)
                    For i As Integer = 0 To EdgeNum - 1
                        Write.Write(CStr(ChainStart(i, 0)) + " " + CStr(ChainStart(i, 1)) + " " + CStr(ChainStart(i, 2)) + vbCrLf)

                        ConverterCodeToFCK(FeatureCode) = Integer.Parse(Key)
                        ConverterFCKToCode(Integer.Parse(Key)) = FeatureCode
                    Next
                Case 3
                    FeatureChain(FeatureCode) = Func.CreateFeatureChainArc(EdgeNum, FeatureChainStartingPoint, Arcs, Name, Key, Depth, IsInside, IsUpside)
                    Dim XMax, XMin, YMax, YMin As Double
                    Dim ZValue As Double
                    Dim Center(1) As Double
                    Center(0) = ArcInfo(EdgeCounter - 1, 0)
                    Center(1) = ArcInfo(EdgeCounter - 1, 1)
                    ZValue = ArcInfo(EdgeCounter - 1, 2)

                    If Math.Cos(StartAngle) > Math.Cos(EndAngle) Then : XMax = Center(0) + Radious * Math.Cos(StartAngle)
                    Else : XMax = Center(0) + Radious * Math.Cos(EndAngle)
                    End If

                    If StartAngle < PI And EndAngle > PI Then
                        XMin = Center(0) + Radious * Math.Cos(PI)
                    Else
                        If Math.Cos(StartAngle) < Math.Cos(EndAngle) Then : XMin = Center(0) + Radious * Math.Cos(StartAngle)
                        Else : XMin = Center(0) + Radious * Math.Cos(EndAngle)
                        End If
                    End If

                    If StartAngle < PI / 2 And EndAngle > PI / 2 Then
                        YMax = Center(1) + Radious * Math.Sin(PI / 2)
                    Else
                        If Math.Sin(StartAngle) > Math.Sin(EndAngle) Then : YMax = Center(1) + Radious * Math.Sin(StartAngle)
                        Else : YMax = Center(1) + Radious * Math.Sin(EndAngle)
                        End If
                    End If

                    If StartAngle < PI * 3 / 2 And EndAngle > PI * 3 / 2 Then
                        YMin = Center(1) + Radious * Math.Sin(PI * 3 / 2)
                    Else
                        If Math.Sin(StartAngle) < Math.Sin(EndAngle) Then : YMin = Center(1) + Radious * Math.Sin(StartAngle)
                        Else : YMin = Center(1) + Radious * Math.Sin(EndAngle)
                        End If
                    End If
                    Write.Write(CStr(FeatureCode) + " " + CStr(CurveType(0)) + " 2" + vbCrLf)
                    Write.Write(CStr(XMax) + " " + CStr(YMax) + " " + CStr(ZValue) + vbCrLf)
                    Write.Write(CStr(XMin) + " " + CStr(YMin) + " " + CStr(ZValue) + vbCrLf)
            End Select
            'Open Edgdeの設定（対象の加工フィーチャの凸エッジをOpenEdgeに設定）
            If FeatureType(FeatureCode) = 2 Or FeatureType(FeatureCode) = 4 Or FeatureType(FeatureCode) = 5 Or FeatureType(FeatureCode) = 6 Then
                For EdgeCounter = 0 To EdgeNum - 1
                    If EdgeType(EdgeCounter) = 1 Then
                        SetOpenEdge(FeatureChain(FeatureCode), EdgeCounter + 1)
                    End If
                Next
            End If
            Write.Write(CStr(Depth) + vbCrLf)

            ConverterCodeToFCK(FeatureCode) = Integer.Parse(Key)
            ConverterFCKToCode(Integer.Parse(Key)) = FeatureCode

        Next

        Write.Close()

    End Sub

    '既にある加工データを読み込み 'MEMO 130812 現在使わない関数
    Sub ReadProcessData()
        '本システムで扱う対象の加工データ
        Dim OperationName As String             'オペレーション名
        Dim MachiningType As Integer            '加工の種類(1:フェースミル加工/2:輪郭加工/3:ポケット加工/4:ドリル加工/5:スパイラル加工)
        Dim ToolID As String                    '工具ID
        Dim SpindleSpeed As Long                '主軸回転数
        Dim XYFeedrate As Double                'XY方向送り速度
        Dim ZFeedrate As Double                 'Z方向送り速度
        Dim MachiningMode As Integer             '加工モード(走査線/一方向)
        Dim MachiningStrategy As Long           '加工方法（アップカット/ダウンカット）
        Dim StepOver As Double                  'ステップオーバー
        Dim TotalDepth As Double                '合計深さ(合計で加工する深さ)
        Dim IncrementalDepth As Double          '増分深さ(Z方向ステップオーバー)
        Dim StartingDepth As Double             '開始深さ(加工開始点の深さ)
        Dim RoughPasses As Integer              '荒パス数（輪郭加工のみ）

        Dim Operation As Esprit.Operation
        Dim Tech As EspritTechnology.Technology
        Dim TechType As Long
        Dim OperationNum As Long
        Dim OperationCounter As Long
        Dim WriteV As IO.StreamWriter
        Dim pathV As String = "ProcessDataView.txt"
        Dim Write As IO.StreamWriter
        Dim path As String = "ProcessData.txt"

        WriteV = New IO.StreamWriter(pathV)
        Write = New IO.StreamWriter(path)

        OperationNum = Document.Operations.Count
        Write.Write(CStr(OperationNum) + vbCrLf)
        WriteV.Write("Number of Operations: " + CStr(OperationNum) + vbCrLf + vbCrLf)

        '加工データを取得
        For OperationCounter = 1 To OperationNum
            Operation = Document.Operations.Item(CInt(OperationCounter))
            Tech = CType(Operation.Technology, EspritTechnology.Technology)
            OperationName = Tech.Name
            TechType = Tech.TechnologyType
            Select Case TechType
                Case 273    'フェースミル加工
                    MachiningType = 1
                Case 274    '輪郭加工
                    MachiningType = 2
                Case 277    'ポケット加工
                    MachiningType = 3
                Case 173    'ドリル加工
                    MachiningType = 4
                Case 275    'スパイラル加工
                    MachiningType = 5
                Case Else   'その他の加工
                    MsgBox("本システムで対応していない加工法です")
                    MachiningType = 0
            End Select
            ToolID = CStr(Tech.Item("ToolID").Value)
            SpindleSpeed = CLng(Tech.Item("SpindleSpeedRPM").Value)
            XYFeedrate = CDbl(Tech.Item("XYFeedratePM").Value)
            ZFeedrate = CDbl(Tech.Item("ZFeedratePM").Value)
            StepOver = CDbl(Tech.Item("StepOver").Value)
            TotalDepth = CDbl(Tech.Item("Totaldepth").Value)
            IncrementalDepth = CDbl(Tech.Item("IncrementalDepth").Value)
            StartingDepth = CDbl(Tech.Item("StartingDepth").Value)
            If MachiningType = 1 Then
                'フェースミル加工の場合
                MachiningStrategy = CLng(Tech.Item("MachiningStrategy").Value)
            Else
                '輪郭加工/ポケット加工の場合
                MachiningStrategy = CLng(Tech.Item("CuttingStrategy").Value)
            End If
            Select Case MachiningType
                Case 1
                    'フェースミル加工の場合
                    MachiningMode = CInt(Tech.Item("MachiningStrategy").Value)
                Case 2
                    '輪郭加工の場合
                    MachiningMode = CInt(Tech.Item("ProcessOreder").Value)
                Case 3
                    'ポケットミル加工の場合
                    MachiningMode = CInt(Tech.Item("ToolMotionPattern").Value)
            End Select
            RoughPasses = 0
            If MachiningMode = 2 Then RoughPasses = CInt(Tech.Item("RoughPasses").Value)

            'ProcessDataViewへの出力
            WriteV.Write("Operation#" + CStr(OperationCounter) + vbCrLf)
            WriteV.Write("OprrationName: " + CStr(OperationName) + vbCrLf)
            WriteV.Write("ToolID: " + CStr(ToolID) + vbCrLf)
            WriteV.Write("SpindleSpeed: " + CStr(SpindleSpeed) + " RPM" + vbCrLf)
            WriteV.Write("XYFeedrate: " + CStr(XYFeedrate) + " mm/mim" + vbCrLf)
            WriteV.Write("ZFeedrate: " + CStr(ZFeedrate) + " mm/min" + vbCrLf)
            WriteV.Write("MachiningMode: " + CStr(MachiningMode) + vbCrLf)
            WriteV.Write("StepOver: " + CStr(StepOver) + " mm" + vbCrLf)
            WriteV.Write("TotalDepth: " + CStr(TotalDepth) + " mm" + vbCrLf)
            WriteV.Write("IncrementalDepth: " + CStr(IncrementalDepth) + " mm" + vbCrLf)
            WriteV.Write("StartingDepth: " + CStr(StartingDepth) + " mm" + vbCrLf)
            WriteV.Write("MachiningStrategy: " + CStr(MachiningStrategy) + vbCrLf)
            WriteV.Write("RoughPasses: " + CStr(RoughPasses) + vbCrLf)
            WriteV.Write(vbCrLf)

            'ProcessDataへの出力
            Write.Write(CStr(OperationCounter) + vbCrLf)
            Write.Write(CStr(OperationName) + vbCrLf)
            Write.Write(CStr(ToolID) + vbCrLf)
            Write.Write(CStr(SpindleSpeed) + vbCrLf)
            Write.Write(CStr(XYFeedrate) + vbCrLf)
            Write.Write(CStr(ZFeedrate) + vbCrLf)
            Write.Write(CStr(MachiningMode) + vbCrLf)
            Write.Write(CStr(StepOver) + vbCrLf)
            Write.Write(CStr(TotalDepth) + vbCrLf)
            Write.Write(CStr(IncrementalDepth) + vbCrLf)
            Write.Write(CStr(StartingDepth) + vbCrLf)
            Write.Write(CStr(MachiningStrategy) + vbCrLf)
            Write.Write(CStr(RoughPasses) + vbCrLf)
            Write.Write(vbCrLf)

        Next
        WriteV.Close()
        Write.Close()


    End Sub

    '素材形状を作成（MaterialShapeFormを起動）
    Sub setMaterialShape()
        MaterialShapeForm = New MaterialShapeForm
        MaterialShapeForm.Show()
    End Sub

    '素材形状を作成
    Sub CreateMaterialShape()
        Dim MaterialXMax As Double
        Dim MaterialXMin As Double
        Dim MaterialYMax As Double
        Dim MaterialYMin As Double
        Dim MaterialZMax As Double
        Dim MaterialZMin As Double
        Dim path As String = "MaterialShape.txt"
        Dim Write As StreamWriter = New StreamWriter(path)

        If MaterialShapeForm.Switch Then
            MaterialXMax = CDbl(XMax + MaterialShapeForm.XPositive)
            MaterialYMax = CDbl(YMax + MaterialShapeForm.YPositive)
            MaterialZMax = CDbl(ZMax + MaterialShapeForm.ZPositive)
            MaterialXMin = CDbl(XMin - MaterialShapeForm.XNegative)
            MaterialYMin = CDbl(YMin - MaterialShapeForm.YNegative)
            MaterialZMin = CDbl(ZMin - MaterialShapeForm.ZNegative)
        Else
            MaterialXMax = CDbl(MaterialShapeForm.XPosition + MaterialShapeForm.XSize)
            MaterialYMax = CDbl(MaterialShapeForm.YPosition + MaterialShapeForm.YSize)
            MaterialZMax = CDbl(MaterialShapeForm.ZPosition + MaterialShapeForm.ZSize)
            MaterialXMin = CDbl(MaterialShapeForm.XPosition)
            MaterialYMin = CDbl(MaterialShapeForm.YPosition)
            MaterialZMin = CDbl(MaterialShapeForm.ZPosition)
        End If

        Write.Write(CStr(MaterialXMax) + " ")
        Write.Write(CStr(MaterialYMax) + " ")
        Write.Write(CStr(MaterialZMax) + " ")
        Write.Write(CStr(MaterialXMin) + " ")
        Write.Write(CStr(MaterialYMin) + " ")
        Write.Write(CStr(MaterialZMin) + vbCrLf)

        'MsgBox(CStr(MaterialXMax) + ", " + CStr(MaterialYMax) + ", " + CStr(MaterialZMax))
        'MsgBox(CStr(MaterialXMax) + ", " + CStr(MaterialYMax) + ", " + CStr(MaterialZMin))
        'MsgBox(CStr(MaterialXMax) + ", " + CStr(MaterialYMin) + ", " + CStr(MaterialZMax))
        'MsgBox(CStr(MaterialXMax) + ", " + CStr(MaterialYMin) + ", " + CStr(MaterialZMin))
        'MsgBox(CStr(MaterialXMin) + ", " + CStr(MaterialYMax) + ", " + CStr(MaterialZMax))
        'MsgBox(CStr(MaterialXMin) + ", " + CStr(MaterialYMax) + ", " + CStr(MaterialZMin))
        'MsgBox(CStr(MaterialXMin) + ", " + CStr(MaterialYMin) + ", " + CStr(MaterialZMax))
        'MsgBox(CStr(MaterialXMin) + ", " + CStr(MaterialYMin) + ", " + CStr(MaterialZMin))

        Write.Close()

    End Sub

    '被削材の情報を決定（MaterialSettingFormを起動）
    Sub SetMaterialInfo()
        MaterialSettingForm = New MaterialSettingForm
        MaterialSettingForm.Show()
    End Sub

    'FeatureCodeの指定
    Sub setFeatureCode()
        FeatureCode = CInt(InputBox("Select FeatureCode"))
        IsFeatureCodeSet = True
        ProcessOrder = 0
        'RoughnessSettingForm = New RoughnessForm
        'RoughnessSettingForm.Show()
    End Sub

    '工具情報の読み込み
    Sub ReadToolInfos()
        Dim path As String = "ToolInfo.txt"
        Dim Reader As IO.StreamReader = New StreamReader(path)
        Dim Read As String
        Dim ToolID(100) As String               '工具ID
        Dim ToolType(100) As Integer            '工具の種類（1:フェースミル/2:スクウェアエンドミル/3:ボールエンドミル）
        Dim ToolDiameter(100) As Double         '工具系
        Dim CuttingLength(100) As Double        '有効刃長
        Dim ToolLength(100) As Double           '工具長
        Dim FlutesNum(100) As Integer           '刃数
        Dim ToolMaterial(100) As Integer        '工具素材（1:カーバイド/2:CBN/3:セラミック/4:HSS）
        Dim Coating(100) As Integer             'コーティング素材（1:あり/0:なし）
        Dim ShankDiameter(100) As Double        'シャンク径
        Dim retval(10) As String
        Dim ToolCounter As Integer = 0
        Dim Technology As EspritTechnology.Technology

        While Not Reader.Peek = -1
            Read = Reader.ReadLine()
            retval = Read.Split(CChar("/"))

            ToolID(ToolCounter) = retval(0)
            ToolType(ToolCounter) = CInt(retval(1))
            ToolDiameter(ToolCounter) = CDbl(retval(2))
            CuttingLength(ToolCounter) = CDbl(retval(3))
            ToolLength(ToolCounter) = CDbl(retval(4))
            FlutesNum(ToolCounter) = CInt(retval(5))
            ToolMaterial(ToolCounter) = CInt(retval(6))
            Coating(ToolCounter) = CInt(retval(7))
            ShankDiameter(ToolCounter) = CDbl(retval(8))

            Technology = Func.SetTool(ToolID(ToolCounter), ToolType(ToolCounter), ToolDiameter(ToolCounter), CuttingLength(ToolCounter), FlutesNum(ToolCounter), ToolLength(ToolCounter), ToolMaterial(ToolCounter), Coating(ToolCounter), ShankDiameter(ToolCounter))
            Document.Tools.Add(Technology)

            ToolCounter += 1
        End While



    End Sub

    '荒加工の加工データ作成（RoughProcessを起動）
    Sub RoughProcess()
        If Not IsFeatureCodeSet Then
            MsgBox("FeatureCodeを設定して下さい")
            setFeatureCode()
        End If
        If ProcessOrder > 0 Then
            If Not CBool(MsgBox("荒加工は設定されています．上書きしますか？", MsgBoxStyle.OkCancel)) Then
                Exit Sub
            End If
        End If
        ProcessOrder = 1
        IsDataReusing = True
        RoughProcessForm = New RoughProcess
        RoughProcessForm.Restricting(Length(FeatureCode))
        RoughProcessForm.PreLoardForm(FeatureCode)
        RoughProcessForm.Show()
    End Sub

    '中仕上げの加工データ作成（SemiFhinishingProcessを起動）
    '140303 いったん保留 →PreLoad etc の関数のFCKとCodeの扱いを変更する必要あり
    Sub SemiFhinishingProcess()
        Dim RoughStockWall As Double
        Dim RoughStockFloor As Double
        If Not IsFeatureCodeSet Then
            MsgBox("FeatureChainKeyを設定して下さい")
            setFeatureCode()
            Exit Sub
        End If
        If ProcessOrder = 0 Then
            MsgBox("先にRoughProcessを作成して下さい．")
            RoughProcess()
            Exit Sub
        End If
        If ProcessOrder > 1 Then
            If Not CBool(MsgBox("中仕上げは設定されています．上書きしますか？", MsgBoxStyle.OkCancel)) Then
                Exit Sub
            End If
        End If
        ProcessOrder = 2
        IsDataReusing = True
        IsSemiFinishing = True
        RoughStockWall = RoughProcessForm.StockAllowanceWall
        RoughStockFloor = RoughProcessForm.StockAllowanceFloor
        SemiFinishingProcessForm = New SemiFinishingProcess
        SemiFinishingProcessForm.PreLoarForm(FeatureCode, RoughStockWall, RoughStockFloor)
        SemiFinishingProcessForm.Show()
    End Sub

    '仕上げ加工の加工データ作成（FinishingProcessを起動）
    '140303 いったん保留 →PreLoad etc の関数のFCKとCodeの扱いを変更する必要あり
    Sub FinishingProcess()
        Dim SemiFinishingStockWall As Double
        Dim SemiFinishingStockFloor As Double
        If Not IsFeatureCodeSet Then
            MsgBox("FeatureChainKeyを設定して下さい")
            setFeatureCode()
            Exit Sub
        End If
        If ProcessOrder = 0 Then
            MsgBox("先にRoughProcessを作成して下さい．")
            RoughProcess()
            Exit Sub
        End If
        If ProcessOrder = 1 Then
            If Not CBool(MsgBox("Semi-FinishingProcessを飛ばしてFinishingProcessを作成しますか？", MsgBoxStyle.OkCancel)) Then
                SemiFhinishingProcess()
                Exit Sub
            End If
        End If
        If ProcessOrder > 2 Then
            If Not CBool(MsgBox("仕上げは設定されています．上書きしますか？", MsgBoxStyle.OkCancel)) Then
                Exit Sub
            End If
        End If
        ProcessOrder = 3
        IsDataReusing = True
        SemiFinishingStockWall = SemiFinishingProcessForm.StockAllowanceWall
        SemiFinishingStockFloor = SemiFinishingProcessForm.StockAllowanceFloor
        FinishingProcessForm = New FinishingProcess
        FinishingProcessForm.PreLoarForm(FeatureCode, SemiFinishingStockWall, SemiFinishingStockFloor)
        FinishingProcessForm.Show()
    End Sub

    '加工データを作成(ここから各種のCreateTchonologyを呼び出し)
    Sub CreateTechnology2()
        Dim OperationName As String             'オペレーション名
        Dim MachiningType As Integer            '加工の種類(1:フェースミル加工/2:輪郭加工/3:ポケット加工/4:ドリル加工/5:スパイラル加工)
        Dim ToolID As String                    '工具ID
        Dim SpindleSpeed As Long                '主軸回転数
        Dim XYFeedrate As Double                'XY方向送り速度
        Dim ZFeedrate As Double                 'Z方向送り速度
        Dim MachiningMode As Integer             '加工モード(走査線/一方向)
        Dim MachiningStrategy As Long           '加工方法（アップカット/ダウンカット）
        Dim StepOver As Double                  'ステップオーバー
        Dim TotalDepth As Double                '合計深さ(合計で加工する深さ)
        Dim IncrementalDepth As Double          '増分深さ(Z方向ステップオーバー)
        Dim StartingDepth As Double             '開始深さ(加工開始点の深さ)
        Dim PassesNum As Integer                'パス数
        Dim StockAllowanceWall As Double        '壁残り代
        Dim StockAllowanceFloor As Double       '床残り代


        If IsDataReusing Then
            Select Case ProcessOrder
                Case 1
                    With RoughProcessForm
                        OperationName = .OperationName
                        MachiningType = .MachiningType
                        ToolID = .ToolID
                        SpindleSpeed = .SpindleSpeed
                        XYFeedrate = .XYFeedrate
                        ZFeedrate = .ZFeedrate
                        MachiningMode = .MachiningMode
                        MachiningStrategy = .MachiningStrategy
                        StepOver = .StepOver
                        TotalDepth = .TotalDepth
                        IncrementalDepth = .IncrementalDepth
                        StartingDepth = .StartingDepth
                        PassesNum = .PassesNum
                        StockAllowanceWall = .StockAllowanceWall
                        StockAllowanceFloor = .StockAllowanceFloor
                    End With
                Case 2
                    With SemiFinishingProcessForm
                        OperationName = .OperationName
                        MachiningType = .MachiningType
                        ToolID = .ToolID
                        SpindleSpeed = .SpindleSpeed
                        XYFeedrate = .XYFeedrate
                        ZFeedrate = .ZFeedrate
                        MachiningMode = .MachiningMode
                        MachiningStrategy = .MachiningStrategy
                        StepOver = .StepOver
                        TotalDepth = .TotalDepth
                        IncrementalDepth = .IncrementalDepth
                        StartingDepth = .StartingDepth
                        PassesNum = .PassesNum
                        StockAllowanceWall = .StockAllowanceWall
                        StockAllowanceFloor = .StockAllowanceFloor
                    End With
                Case 3
                    With FinishingProcessForm
                        OperationName = .OperationName
                        MachiningType = .MachiningType
                        ToolID = .ToolID
                        SpindleSpeed = .SpindleSpeed
                        XYFeedrate = .XYFeedrate
                        ZFeedrate = .ZFeedrate
                        MachiningMode = .MachiningMode
                        MachiningStrategy = .MachiningStrategy
                        StepOver = .StepOver
                        TotalDepth = .TotalDepth
                        IncrementalDepth = .IncrementalDepth
                        StartingDepth = .StartingDepth
                        PassesNum = .PassesNum
                        StockAllowanceWall = .StockAllowanceWall
                        StockAllowanceFloor = .StockAllowanceFloor
                    End With
            End Select

        Else
            OperationName = ProcessDataForm.OperationName
            MachiningType = ProcessDataForm.MachiningType
            ToolID = ProcessDataForm.ToolID
            SpindleSpeed = ProcessDataForm.SpindleSpeed
            XYFeedrate = ProcessDataForm.XYFeedrate
            ZFeedrate = ProcessDataForm.ZFeedrate
            MachiningMode = ProcessDataForm.MachiningMode
            MachiningStrategy = ProcessDataForm.MachiningStrategy
            StepOver = ProcessDataForm.StepOver
            TotalDepth = ProcessDataForm.TotalDepth
            IncrementalDepth = ProcessDataForm.IncrementalDepth
            StartingDepth = ProcessDataForm.StartingDepth
            PassesNum = ProcessDataForm.PassesNum
            StockAllowanceWall = ProcessDataForm.StockAllowanceWall
            StockAllowanceFloor = ProcessDataForm.StockAllowanceFloor
        End If

        Dim Key As Integer

        Select Case MachiningType
            Case 1
                'フェースミル加工
                Key = ConverterCodeToFCK(FeatureCode)
                Func.CreateMillFaceTechnology(CStr(Key), OperationName, ToolID, SpindleSpeed, XYFeedrate, ZFeedrate, MachiningMode, MachiningStrategy, StepOver, TotalDepth, IncrementalDepth, StartingDepth, StockAllowanceFloor)
            Case 2
                '輪郭加工
                Key = ConverterCodeToFCK(FeatureCode)
                Func.CreateMillContourTechnology(CStr(Key), OperationName, ToolID, SpindleSpeed, XYFeedrate, ZFeedrate, MachiningMode, MachiningStrategy, StepOver, TotalDepth, IncrementalDepth, StartingDepth, PassesNum, StockAllowanceWall, StockAllowanceFloor)
            Case 3
                'ポケット加工
                Key = ConverterCodeToFCK(FeatureCode)
                Func.CreateMillPocketTechnology(CStr(Key), OperationName, ToolID, SpindleSpeed, XYFeedrate, ZFeedrate, MachiningMode, MachiningStrategy, StepOver, TotalDepth, IncrementalDepth, StartingDepth, StockAllowanceWall, StockAllowanceFloor)
            Case 4
                'ドリル加工
                Key = ConverterCodeToFPK(FeatureCode)
                Func.CreateDrillTechnology(CStr(Key), OperationName, ToolID, SpindleSpeed, ZFeedrate, TotalDepth, StartingDepth)
            Case 5
                'スパイラル加工
                Key = ConverterCodeToFPK(FeatureCode)
                Func.CreateMillSpiralTechnology(CStr(Key), OperationName, ToolID, SpindleSpeed, XYFeedrate, ZFeedrate, MachiningMode, MachiningStrategy, StepOver, TotalDepth, IncrementalDepth, StartingDepth)
        End Select
    End Sub

    '加工データの記録
    Sub RecProcessData()
        Dim ReaderDBM As IO.StreamReader
        Dim path As String
        Dim Read As String
        Dim retval(10) As String
        Dim Code As Integer
        Dim FeatureCode As Integer

        '保存するモデル/フィーチャの情報を取得
        path = "DatabaseManager.txt"
        ReaderDBM = New IO.StreamReader(path)

        'このモデルのコードを読み込み
        Read = ReaderDBM.ReadLine
        retval = Read.Split(CChar(" "))

        Code = CInt(retval(0))

        '加工データの宣言
        Dim OperationName As String             'オペレーション名
        Dim MachiningType As Integer            '加工の種類(1:フェースミル加工/2:輪郭加工/3:ポケット加工/4:ドリル加工/5:スパイラル加工)
        Dim ToolID As String                    '工具ID
        Dim SpindleSpeed As Long                '主軸回転数
        Dim XYFeedrate As Double                'XY方向送り速度
        Dim ZFeedrate As Double                 'Z方向送り速度
        Dim MachiningMode As Integer            '加工モード(走査線/一方向)
        Dim MachiningStrategy As Long           '加工方法（アップカット/ダウンカット）
        Dim StepOver As Double                  'ステップオーバー
        Dim TotalDepth As Double                '合計深さ(合計で加工する深さ)
        Dim IncrementalDepth As Double          '増分深さ(Z方向ステップオーバー)
        Dim StartingDepth As Double             '開始深さ(加工開始点の深さ)
        Dim PassesNum As Integer                '荒パス数（輪郭加工のみ）
        Dim StockAllowanceWall As Double        '壁許容残り代
        Dim StockAllownaceFloor As Double       '床許容残り代




        '荒加工データの保存------------------------------------------------------------------------------------------------------------------
        path = "RoughProcessData.txt"
        Dim ReaderNew As IO.StreamReader = New IO.StreamReader(path)

        path = "RoughProcess Database.txt"
        Dim Writer As IO.StreamWriter = New IO.StreamWriter(path, True)

        'フィーチャCode
        Read = ReaderNew.ReadLine()
        FeatureCode = CInt(Read)

        'オペレーション名
        Read = ReaderNew.ReadLine()
        OperationName = Read

        '加工の種類
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningType = CInt(retval(0))

        '工具ID
        Read = ReaderNew.ReadLine()
        ToolID = Read

        '回転数
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        SpindleSpeed = CLng(retval(0))

        'XY送り速度
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        XYFeedrate = CDbl(retval(0))

        'Z送り速度
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        ZFeedrate = CDbl(retval(0))

        '加工モード
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningMode = CInt(retval(0))

        'ステップオーバー
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        StepOver = CDbl(retval(0))

        '増分深さ
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        IncrementalDepth = CDbl(retval(0))

        '深さ合計
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        TotalDepth = CDbl(retval(0))

        'スタート深さ
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        StartingDepth = CDbl(retval(0))

        '加工方法
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningStrategy = CInt(retval(0))

        'パス数
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        PassesNum = CInt(retval(0))

        '壁許容残り代
        Read = ReaderNew.ReadLine()
        retval = Read.Split(CChar(" "))
        StockAllowanceWall = CDbl(retval(0))

        '床許容残り代
        Read = ReaderNew.ReadLine
        retval = Read.Split(CChar(" "))
        StockAllownaceFloor = CDbl(retval(0))

        '記録の実行
        '値の記録
        Writer.Write(CStr(Code) + " " + CStr(FeatureCode) + vbCrLf)
        Writer.Write(OperationName + vbCrLf)
        Writer.Write(CStr(MachiningType) + vbCrLf)
        Writer.Write(ToolID + vbCrLf)
        Writer.Write(CStr(SpindleSpeed) + vbCrLf)
        Writer.Write(CStr(XYFeedrate) + vbCrLf)
        Writer.Write(CStr(ZFeedrate) + vbCrLf)
        Writer.Write(CStr(MachiningMode) + vbCrLf)
        Writer.Write(CStr(StepOver) + vbCrLf)
        Writer.Write(CStr(TotalDepth) + vbCrLf)
        Writer.Write(CStr(IncrementalDepth) + vbCrLf)
        Writer.Write(CStr(StartingDepth) + vbCrLf)
        Writer.Write(CStr(MachiningStrategy) + vbCrLf)
        Writer.Write(CStr(PassesNum) + vbCrLf)
        Writer.Write(CStr(StockAllowanceWall) + vbCrLf)
        Writer.Write(CStr(StockAllownaceFloor) + vbCrLf)
        Writer.Write(vbCrLf)
        Writer.Close()
        '------------------------------------------------------------------------------------------------------------------------------------




        '中仕上げ加工データの保存------------------------------------------------------------------------------------------------------------------
        path = "SemiFinishingProcessData.txt"
        Dim ReaderNewSF As IO.StreamReader = New IO.StreamReader(path)

        path = "SemiFinishingProcess Database.txt"
        Dim WriterSF As IO.StreamWriter = New IO.StreamWriter(path, True)

        'フィーチャCode
        Read = ReaderNewSF.ReadLine()
        FeatureCode = CInt(Read)

        'オペレーション名
        Read = ReaderNewSF.ReadLine()
        OperationName = Read

        '加工の種類
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningType = CInt(retval(0))

        '工具ID
        Read = ReaderNewSF.ReadLine()
        ToolID = Read

        '回転数
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        SpindleSpeed = CLng(retval(0))

        'XY送り速度
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        XYFeedrate = CDbl(retval(0))

        'Z送り速度
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        ZFeedrate = CDbl(retval(0))

        '加工モード
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningMode = CInt(retval(0))

        'ステップオーバー
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        StepOver = CDbl(retval(0))

        '増分深さ
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        IncrementalDepth = CDbl(retval(0))

        '深さ合計
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        TotalDepth = CDbl(retval(0))

        'スタート深さ
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        StartingDepth = CDbl(retval(0))

        '加工方法
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningStrategy = CInt(retval(0))

        'パス数
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        PassesNum = CInt(retval(0))

        '壁許容残り代
        Read = ReaderNewSF.ReadLine()
        retval = Read.Split(CChar(" "))
        StockAllowanceWall = CDbl(retval(0))

        '床許容残り代
        Read = ReaderNewSF.ReadLine
        retval = Read.Split(CChar(" "))
        StockAllownaceFloor = CDbl(retval(0))

        '記録の実行
        '値の記録
        WriterSF.Write(CStr(Code) + " " + CStr(FeatureCode) + vbCrLf)
        WriterSF.Write(OperationName + vbCrLf)
        WriterSF.Write(CStr(MachiningType) + vbCrLf)
        WriterSF.Write(ToolID + vbCrLf)
        WriterSF.Write(CStr(SpindleSpeed) + vbCrLf)
        WriterSF.Write(CStr(XYFeedrate) + vbCrLf)
        WriterSF.Write(CStr(ZFeedrate) + vbCrLf)
        WriterSF.Write(CStr(MachiningMode) + vbCrLf)
        WriterSF.Write(CStr(StepOver) + vbCrLf)
        WriterSF.Write(CStr(TotalDepth) + vbCrLf)
        WriterSF.Write(CStr(IncrementalDepth) + vbCrLf)
        WriterSF.Write(CStr(StartingDepth) + vbCrLf)
        WriterSF.Write(CStr(MachiningStrategy) + vbCrLf)
        WriterSF.Write(CStr(PassesNum) + vbCrLf)
        WriterSF.Write(CStr(StockAllowanceWall) + vbCrLf)
        WriterSF.Write(CStr(StockAllownaceFloor) + vbCrLf)
        WriterSF.Write(vbCrLf)
        WriterSF.Close()

        '------------------------------------------------------------------------------------------------------------------------------------




        '仕上げ加工データの保存------------------------------------------------------------------------------------------------------------------
        path = "FinishingProcessData.txt"
        Dim ReaderNewF As IO.StreamReader = New IO.StreamReader(path)

        path = "FinishingProcesss Database.txt"
        Dim WriterF As IO.StreamWriter = New IO.StreamWriter(path, True)

        'フィーチャCode
        Read = ReaderNewF.ReadLine()
        FeatureCode = CInt(Read)

        'オペレーション名
        Read = ReaderNewF.ReadLine()
        OperationName = Read

        '加工の種類
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningType = CInt(retval(0))

        '工具ID
        Read = ReaderNewF.ReadLine()
        ToolID = Read

        '回転数
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        SpindleSpeed = CLng(retval(0))

        'XY送り速度
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        XYFeedrate = CDbl(retval(0))

        'Z送り速度
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        ZFeedrate = CDbl(retval(0))

        '加工モード
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningMode = CInt(retval(0))

        'ステップオーバー
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        StepOver = CDbl(retval(0))

        '増分深さ
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        IncrementalDepth = CDbl(retval(0))

        '深さ合計
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        TotalDepth = CDbl(retval(0))

        'スタート深さ
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        StartingDepth = CDbl(retval(0))

        '加工方法
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        MachiningStrategy = CInt(retval(0))

        'パス数
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        PassesNum = CInt(retval(0))

        '壁許容残り代
        Read = ReaderNewF.ReadLine()
        retval = Read.Split(CChar(" "))
        StockAllowanceWall = CDbl(retval(0))

        '床許容残り代
        Read = ReaderNewF.ReadLine
        retval = Read.Split(CChar(" "))
        StockAllownaceFloor = CDbl(retval(0))
        '記録の実行
        '値の記録
        WriterF.Write(CStr(Code) + " " + CStr(FeatureCode) + vbCrLf)
        WriterF.Write(OperationName + vbCrLf)
        WriterF.Write(CStr(MachiningType) + vbCrLf)
        WriterF.Write(ToolID + vbCrLf)
        WriterF.Write(CStr(SpindleSpeed) + vbCrLf)
        WriterF.Write(CStr(XYFeedrate) + vbCrLf)
        WriterF.Write(CStr(ZFeedrate) + vbCrLf)
        WriterF.Write(CStr(MachiningMode) + vbCrLf)
        WriterF.Write(CStr(StepOver) + vbCrLf)
        WriterF.Write(CStr(TotalDepth) + vbCrLf)
        WriterF.Write(CStr(IncrementalDepth) + vbCrLf)
        WriterF.Write(CStr(StartingDepth) + vbCrLf)
        WriterF.Write(CStr(MachiningStrategy) + vbCrLf)
        WriterF.Write(CStr(PassesNum) + vbCrLf)
        WriterF.Write(CStr(StockAllowanceWall) + vbCrLf)
        WriterF.Write(CStr(StockAllownaceFloor) + vbCrLf)
        WriterF.Write(vbCrLf)
        WriterF.Close()

        '------------------------------------------------------------------------------------------------------------------------------------




        '        '被引用度の書き込み------------------------------------------------------------------------------------------------------------------
        '        path = "Ref Database.txt"
        '        Dim WriterRef As IO.StreamWriter = New IO.StreamWriter(path, True)
        '
        '        '書き込みの実行（被引用度，合計回数は０で書き込み）
        '        WriterRef.Write(CStr(Code) + " " + CStr(FeatureCode) + " 0 0" + vbCrLf)
        '        WriterRef.Close()
        '
        '        '------------------------------------------------------------------------------------------------------------------------------------


        '粗さ情報の書き込み--------------------------------------------------------------------------------------------------------------------
        path = "Roughness Database.txt"
        Dim WriterRou As IO.StreamWriter = New StreamWriter(path)
        Dim RoughnessValue As Double = RoughnessSettingForm.Roughness

        '書き込みの実行
        WriterRou.Write(CStr(Code) + " " + CStr(FeatureCode) + " " + CStr(RoughnessValue) + vbCrLf)
        WriterRou.Close()

        '-------------------------------------------------------------------------------------------------------------------------------------

        '素材情報の書き込み------------------------------------------------------------------------------------------------------------------
        path = "Material Database.txt"
        Dim WriterMat As IO.StreamWriter = New StreamWriter(path, True)
        Dim MaterialValue As Double = MaterialSettingForm.MaterialValue

        '書き込みの実行
        WriterMat.Write(CStr(Code) + " " + CStr(FeatureCode) + " " + CStr(MaterialValue) + vbCrLf)
        WriterMat.Close()

        '------------------------------------------------------------------------------------------------------------------------------------

        '使用した工具情報の書き込み------------------------------------------------------------------------------------------------------------
        path = " ToolOfProcess Database.txt"
        Dim WriterTool As IO.StreamWriter = New StreamWriter(path)
        Dim ToolTypeValue As Integer = RoughProcessForm.usedToolType
        Dim ToolDiameterValue As Double = RoughProcessForm.usedToolDiameter

        '書き込みの実行
        WriterTool.Write(CStr(Code) + " " + CStr(FeatureCode) + " " + CStr(ToolTypeValue) + " " + CStr(ToolDiameterValue) + vbCrLf)
        WriterMat.Close()

    End Sub

    '新しい工具を作成（ToolSettingFormを起動）
    Sub ToolSetting()
        NewToolForm = New NewToolForm
        NewToolForm.Show()
    End Sub

    '新しい工具を作成（実際にTechを作成）
    Sub ToolCreating()
        Dim ToolID As String               '工具ID
        Dim ToolType As Integer            '工具の種類（1:フェースミル/2:スクウェアエンドミル/3:ボールエンドミル）
        Dim ToolDiameter As Double         '工具系
        Dim CuttingLength As Double        '有効刃長
        Dim ToolLength As Double           '工具長
        Dim FlutesNum As Integer           '刃数
        Dim ToolMaterial As Integer        '工具素材（1:カーバイド/2:CBN/3:セラミック/4:HSS）
        Dim Coating As Integer             'コーティング素材（1:あり/0:なし）
        Dim ShankDiameter As Double         'シャンク径
        Dim Technology As EspritTechnology.Technology

        '値の取得
        ToolID = NewToolForm.ToolID
        ToolType = NewToolForm.ToolType
        ToolDiameter = NewToolForm.ToolDiameter
        CuttingLength = NewToolForm.CuttingLength
        ToolLength = NewToolForm.ToolLength
        FlutesNum = NewToolForm.FlutesNum
        ToolMaterial = NewToolForm.ToolMaterial
        Coating = NewToolForm.Coating
        ShankDiameter = NewToolForm.ShankDiameter

        Technology = Func.SetTool(ToolID, ToolType, ToolDiameter, CuttingLength, FlutesNum, ToolLength, ToolMaterial, Coating, ShankDiameter)
        Document.Tools.Add(Technology)


        'ToolInfo.txtに書き込み
        Dim path As String = "ToolInfo.txt"
        Dim Writer As StreamWriter = New StreamWriter(path, True)

        Writer.Write(ToolID + "/" + CStr(ToolType) + "/" + CStr(ToolDiameter) + "/" + CStr(CuttingLength) + "/" + CStr(ToolLength) + "/" + CStr(FlutesNum) + "/" + CStr(ToolMaterial) + "/" + CStr(Coating) + vbCrLf)

        Writer.Close()


    End Sub

    'only for debuging
    Sub Debug()
        Dim FeatureChain As Esprit.FeatureChain
        FeatureChain = Document.FeatureChains.Item(10)
        Dim path As String = "DebugB.txt"
        Dim Writer As IO.StreamWriter = New IO.StreamWriter(path)

        With FeatureChain
            Writer.Write(.Area)
            Writer.Write(vbCrLf)
            Writer.Write(.BoundingBoxLength)
            Writer.Write(vbCrLf)
            Writer.Write(.BoundingBoxWidth)
            Writer.Write(vbCrLf)
            Writer.Write(.Color)
            Writer.Write(vbCrLf)
            Writer.Write(.ComGraphicObject)
            Writer.Write(vbCrLf)
            Writer.Write(.Count)
            Writer.Write(vbCrLf)
            Writer.Write(.CustomProperties)
            Writer.Write(vbCrLf)
            Writer.Write(.CutSide)
            Writer.Write(vbCrLf)
            Writer.Write(.Depth)
            Writer.Write(vbCrLf)
            Writer.Write(.Document)
            Writer.Write(vbCrLf)
            Writer.Write(.DraftAngle)
            Writer.Write(vbCrLf)
            Writer.Write(.GraphicObjectType)
            Writer.Write(vbCrLf)
            Writer.Write(.GraphicsCollectionIndex)
            Writer.Write(vbCrLf)
            Writer.Write(.Grouped)
            Writer.Write(vbCrLf)
            Writer.Write(.GuiTypeName)
            Writer.Write(vbCrLf)
            Writer.Write(.IsClosed)
            Writer.Write(vbCrLf)
            Writer.Write(.Key)
            Writer.Write(vbCrLf)
            Writer.Write(.Layer)
            Writer.Write(vbCrLf)
            'Writer.Write(.LeadInPoint)+ vbCrLf)
            'Writer.Write(.LeadOutPoint)+ vbCrLf)
            Writer.Write(.Length)
            Writer.Write(vbCrLf)
            Writer.Write(.LineType)
            Writer.Write(vbCrLf)
            Writer.Write(.LineWeight)
            Writer.Write(vbCrLf)
            Writer.Write(.MaterialRemoval)
            Writer.Write(vbCrLf)
            Writer.Write(.MaxZ)
            Writer.Write(vbCrLf)
            Writer.Write(.MinZ)
            Writer.Write(vbCrLf)
            Writer.Write(.Name)
            Writer.Write(vbCrLf)
            Writer.Write(.Plane)
            Writer.Write(vbCrLf)
            Writer.Write(.SmallestRadius)
            Writer.Write(vbCrLf)
            Writer.Write(.Through)
            Writer.Write(vbCrLf)
            'Writer.Write(.Type)
            Writer.Write(vbCrLf)
            Writer.Write(.TypeName)
            Writer.Write(vbCrLf)
            Writer.Write(.WorkCoordinate)
            Writer.Write(vbCrLf)
        End With

        Writer.Close()
    End Sub

    Sub SetOpenEdge2()
        ' On Error GoTo ErrorHandler
Start:
        Dim Inum As Integer
        Dim F1 As Esprit.FeatureChain
        F1 = CType(Document.GetAnyElementItem("Select FeatureChain Sub Element", Inum, EspritConstants.espGraphicObjectType.espFeatureChain), Esprit.FeatureChain)

        Dim ComFeature1 As EspritFeatures.ComFeature
        ComFeature1 = CType(F1, EspritFeatures.ComFeature)

        Dim CustomProperties As EspritProperties.CustomProperties
        CustomProperties = CType(ComFeature1.Item(Inum).CustomProperties, EspritProperties.CustomProperties)

        Call CustomProperties.AssignByEnumeration(EspritProperties.espReservedCustomPropertyType.espFeatureItemOpenEdgeProperty, True)
        Document.Refresh()
        ' GoTo Start

ErrorHandler:

    End Sub

    Sub SetOpenEdge(ByVal FeatureChain As Esprit.FeatureChain, ByVal SegmentNum As Integer)

        Dim Inum As Integer = SegmentNum
        Dim F1 As Esprit.FeatureChain
        F1 = FeatureChain

        Dim ComFeature1 As EspritFeatures.ComFeature
        ComFeature1 = CType(F1.ComGraphicObject, EspritFeatures.ComFeature)

        Dim CustomProperties As EspritProperties.CustomProperties
        CustomProperties = CType(ComFeature1.Item(Inum).CustomProperties, EspritProperties.CustomProperties)

        Call CustomProperties.AssignByEnumeration(EspritProperties.espReservedCustomPropertyType.espFeatureItemOpenEdgeProperty, True)
        Document.Refresh()

    End Sub


    '以下使用しない関数------------------------------------------------------------------------------------------------------------------
    '新しい加工データの数値を設定（ProcessdataFormを起動）
    Sub CreateTechnology1()
        IsDataReusing = False
        FeatureCode = CInt(InputBox("Select FeatureChain"))
        ProcessDataForm = New ProcessDataForm
        ProcessDataForm.PreLoadForm(FeatureCode)
        ProcessDataForm.Show()
    End Sub

End Class